package com.ycwl.aiLive_2d.exception;

import com.ycwl.aiLive_2d.utils.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

/**
 * @date 2022年09月23日 10:19
 * 全局异常处理器
 */
@RestControllerAdvice(basePackages = {"com.ycwl.aiLanguage"})
public class CustomExceptionHandle {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomExceptionHandle.class);

    @ExceptionHandler(BaseException.class)
    public ApiResponse baseExceptionHandler(HttpServletResponse response, BaseException ex) {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        return ApiResponse.buildResponse(ex.getStatus(), ex.getMessage());
    }

    /**
     * 异常统一返回处理
     */
    @ExceptionHandler(value = Exception.class)
    public ApiResponse<String> handle(Exception e) {
        LOGGER.error("系统异常 -> {}", e.getMessage(), e);
        return ApiResponse.buildResponse(500, "服务器异常");
    }


}
